package com.totoro.tygeneratedragonblock.commands;

import com.totoro.tygeneratedragonblock.TyGenerateDragonBlock;
import com.totoro.tygeneratedragonblock.constants.CommandConst;
import com.totoro.tygeneratedragonblock.enums.CommandEnum;
import com.totoro.tygeneratedragonblock.executor.command.MainCommandExecutor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/10 - 19:45
 * @description
 **/
public class MainCommand implements CommandExecutor, TabExecutor {

    private final TyGenerateDragonBlock plugin;

    public MainCommand(TyGenerateDragonBlock plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(args.length == 0) {
            CommandEnum.HELP_COMMAND.getMainCommandExecutor().execute(plugin, sender, args);
            return false;
        }
        CommandEnum commandEnum = CommandEnum.getValue(args[0]);
        MainCommandExecutor mainCommandExecutor = commandEnum.getMainCommandExecutor();
        mainCommandExecutor.execute(plugin, sender, args);
        return false;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        if(!sender.isOp()) {
            return null;
        }
        if(args.length == 1) {
            if(args[0].startsWith("r")) {
                if(args[0].startsWith("re")) {
                    if(args[0].startsWith("rel")) {
                        return Collections.singletonList(CommandConst.RELOAD);
                    }
                    if(args[0].startsWith("rem")) {
                        return Collections.singletonList(CommandConst.REMOVE);
                    }
                    return Arrays.asList(CommandConst.RELOAD, CommandConst.REMOVE);
                }
                return Arrays.asList(CommandConst.RELOAD, CommandConst.REMOVE);
            }
            if(args[0].startsWith("h")) {
                return Collections.singletonList(CommandConst.HELP);
            }
            if(args[0].startsWith("s")) {
                return Collections.singletonList(CommandConst.SPAWN);
            }
        }

        if(args.length == 2) {
            if(args[0].equalsIgnoreCase(CommandConst.REMOVE)) {
                Set<String> spawnIds = plugin.getSpawnersManager().getSpawnPoints().keySet();
                if(spawnIds.isEmpty()) {
                    return null;
                }
                return new ArrayList<>(spawnIds);
            }
            if(args[0].equalsIgnoreCase(CommandConst.SPAWN)) {
                Set<String> blockIds = plugin.getBlocksManager().getBlockDefinitions().keySet();
                if(blockIds.isEmpty()) {
                    return null;
                }
                return new ArrayList<>(blockIds);
            }
        }
        return Arrays.stream(CommandEnum.values()).map(CommandEnum::getCommandArg).collect(Collectors.toList());
    }
}
